package com.xiaoxu.springboot.modules.test.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.xiaoxu.springboot.modules.common.vo.Result;
import com.xiaoxu.springboot.modules.common.vo.Search;
import com.xiaoxu.springboot.modules.test.entity.City;
import com.xiaoxu.springboot.modules.test.service.CityService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     *http://localhost:8183/api/city/insert
     * { "cityName":"xiaoxu","localCityName":"ahh","countryId":"522"}
     * @param city
     * @return
     */
    @PostMapping(value = "/city/insert",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }

    /**
     *http://localhost:8183/api/city/update
     *  cityName:xixixi  localCityName:hah   countryId:441
     * @param city
     * @return
     */
    @PutMapping(value ="/city/update",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /**
     * http://localhost:8183/api/city/2259----xuanBao
     * @param cityId
     * @return
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCityByCityId(@PathVariable Integer cityId){
        return cityService.deleteCityByCityId(cityId);
    }

    /**
     *http://localhost:8183/api/city/v1/2260
     * @param cityId
     * @return
     */
    @GetMapping("/city/v1/{cityId}")
    public City selectCityByCityId(@PathVariable Integer cityId){
        return cityService.selectCityByCityId(cityId);
    }

    /**
     *
     * @param cityName
     * @param localCityName
     * @return
     */
    @GetMapping("/city")
    public City getCityByNames(@RequestParam("cityName") String cityName, @RequestParam("localCityName") String localCityName){
        return cityService.getCityByNames(cityName,localCityName);

    }

    /**
     *http://localhost:8183/api/city/v2/482
     * @param countryId
     * @return
     */
    @GetMapping("/city/v2/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable Integer countryId){
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * {"currentPage":1,"pageSize":5,"sort":"city_name","direction":"desc","keyword":""}
     * @param search
     * @return
     */
    @PutMapping(value = "/cities/v1" ,consumes = "application/json")
    public PageInfo<City> getCitiesBySeach(@RequestBody Search search){
        return cityService.getCitiesBySeach(search);
    }

    /**
     *
     * @param search
     * @return
     */
    @PostMapping(value = "/cities/v2",consumes = "application/json")
    public Page<City> getCitiesBySeach1(@RequestBody Search search){
        return cityService.getCitiesBySeach1(search);
    }

}
